package com.jiazhong.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiazhong.backend.service.UserService;
import com.jiazhong.commons.Mapper.UserMapper;
import com.jiazhong.commons.Util.JsonResult;
import com.jiazhong.commons.Util.ResultTool;
import com.jiazhong.commons.Util.StringUtil;
import com.jiazhong.commons.bean.User;
import com.jiazhong.commons.exception.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public JsonResult find(int page, int size) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("id","username","phone","email","address","status");
        Page<User> of = Page.of(page,size);
        List<User> list = list(of,wrapper);
        if (list==null||list.size()==0){
            throw new UserNotFoundDataException("没有查询出来数据");
        }
        of.setRecords(list);
        return ResultTool.success(of);
    }

    @Override
    public JsonResult findSearch(String name, int page, int size) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("id","username","phone","email","address","status");
        wrapper.like("username",name);
        Page<User> of = Page.of(page,size);
        List<User> list = list(of,wrapper);
        if (list==null||list.size()==0){
            throw new UserNotFoundDataException("没有查询出来数据");
        }
        of.setRecords(list);
        return ResultTool.success(of);
    }

    @Override
    public JsonResult saveUser(User user) {
        boolean b = save(user);
        if (!b){
            throw new UserSaveException("添加数据失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult removeUser(String id) {
        boolean b = removeById(id);
        if (!b) {
            throw new UserRemoveException("用户删除失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult findExists(String column, String value) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq(column, value);
        wrapper.select("count(0)");
        Map<String, Object> map = getMap(wrapper);
        int row = Integer.parseInt(map.get("count(0)").toString());
        if (row > 0) {
            throw new UserExistsException(column + "已经存在");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult updateUserPassword(String id, String oldPass, String newPass) {
        //判断原始密码是否正确
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        wrapper.eq("password",StringUtil.encryptMD5(oldPass));
        User one = getOne(wrapper);
        if (one == null){
            throw new InputPasswordFailException("原始密码输入错误");
        }
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("password",StringUtil.encryptMD5(newPass));
        boolean b = update(updateWrapper);
        if (!b){
            throw new UserUpdateException("修改用户密码错误");
        }
        return ResultTool.success("success");
    }

}